package com.dimong.www.loan.db;

import android.content.Context;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by wuligang on 2018/5/29.
 */

public class CheckDao {
    private Context context;
    private Dao<CheckModel, Integer> dao;

    public CheckDao(Context context) {
        this.context = context;
        try {
            this.dao = OpenHelperManager.getHelper(context,
                LoanDBHelper.class).getDao(CheckModel.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void insert(CheckModel data) {
        try {
            dao.createOrUpdate(data);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 查询user表中的所有数据
    public List<CheckModel> selectAll() {
        List<CheckModel> users = null;
        try {
            users = dao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    public List<CheckModel> getCheckList(long offset) {
        List<CheckModel> checkModels = null;
        try {
            QueryBuilder queryBuilder = dao.queryBuilder();
            queryBuilder.offset(offset); //偏移量
            queryBuilder.limit(10l); //最多几行  offset + limit 做分页
            queryBuilder.orderBy("time", false);
            queryBuilder.orderBy("create_time", false);
            checkModels = queryBuilder.query();//此方法相当于build，提交设置
        } catch (Exception e) {

        }
        return checkModels;
    }

    public List<CheckModel> getCheckListByMonth(int year, int month, long offset) {
        List<CheckModel> checkModels = null;
        try {
            QueryBuilder queryBuilder = dao.queryBuilder();
            queryBuilder.offset(offset); //偏移量
            queryBuilder.limit(10l); //最多几行  offset + limit 做分页
            queryBuilder.orderBy("time", false);
            queryBuilder.orderBy("create_time", false);
            queryBuilder.where().eq("year", year); //多条件查询
            queryBuilder.where().eq("month", month); //多条件查询
            checkModels = queryBuilder.query();//此方法相当于build，提交设置
        } catch (Exception e) {

        }
        return checkModels;
    }


    public void deletaAll() {
        try {
            dao.executeRawNoArgs("delete from check");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
